package com.atguigu.ucenterservice.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.commonutils.MD5Utils;
import com.atguigu.servicebase.handler.GuliException;
import com.atguigu.ucenterservice.entity.*;
import com.atguigu.ucenterservice.mapper.UcenterMemberMapper;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author fxy
 * @since 2022-05-11
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //登录
    @Override
    public String login(LoginVo loginVo) {
        //member对象里面有手机号和密码

        //获取登录的手机号和密码
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        //手机号和密码非空的判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登陆失败!!!");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(wrapper);
        //判断查出来阿对象是否为空
        if (member==null){//没有这个手机号
            throw new GuliException(20001,"登陆失败!!!");
        }

        //判断密码
        //存储到数据库的密码是加密的
        //把输入的密码进行加密，在与数据库中的密码进行加密
        if (!MD5Utils.md5(password).equals(MD5Utils.md5(password))){
            throw new GuliException(20001,"登陆失败!!!");
        }

        //判断用户是否被禁用
        if (member.getIsDisabled()){
            throw new GuliException(20001,"登陆失败!!!");
        }

        //登陆成功
        //生成token字符串。使用jwt
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }
    //修改保存
    @Override
    public void saveVo(RegisterSaveVo registerSaveVo) {
        //获取code
        String id = registerSaveVo.getId();
        String code = registerSaveVo.getCode();
        String mobile = registerSaveVo.getMobile();
        String nickname = registerSaveVo.getNickname();
        String password = registerSaveVo.getPassword();
        //非空的判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)){
            throw new GuliException(20001,"修改失败!!!");
        }
        //判断验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new GuliException(20001,"验证码错误!!!");
        }
        //判断是否修改手机号
        UcenterMember ucenterMember1 = baseMapper.selectById(id);
        String mobileOld = ucenterMember1.getMobile();
        //不修改手机号
        if (mobile.equals(mobileOld)){
            //判断手机号不能重复 (是否重复)
            QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
            wrapper.eq("mobile",mobile);
            Integer count = baseMapper.selectCount(wrapper);
            if (count>1){
                throw new GuliException(20001,"手机号重复!!!");
            }
        }//修改手机号
        else {
            //判断手机号不能重复 (是否重复)
            QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
            wrapper.eq("mobile", mobile);
            Integer count = baseMapper.selectCount(wrapper);
            if (count > 0) {
                throw new GuliException(20001, "手机号重复!!!");
            }
        }
        //修改保存至数据库
        UcenterMember ucenterMember=new UcenterMember();
        BeanUtils.copyProperties(registerSaveVo,ucenterMember);
        //判读密码是否需要加密
        if (!StringUtils.isEmpty(password)){
            ucenterMember.setPassword(MD5Utils.md5(password));
        }
        //修改保存
      baseMapper.updateById(ucenterMember);
    }
    //注册
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile(); //手机号
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码

        //非空的判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code)
                || StringUtils.isEmpty(password)|| StringUtils.isEmpty(nickname)){
            throw new GuliException(20001,"注册失败!!!");
        }

        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new GuliException(20001,"注册失败!!!");
        }

        //判断手机号不能重复 (是否注册)
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new GuliException(20001,"注册失败!!!");
        }

        //数据添加到数据库中
        UcenterMember ucenterMember=new UcenterMember();
        BeanUtils.copyProperties(registerVo,ucenterMember);
        ucenterMember.setPassword(MD5Utils.md5(password));
        baseMapper.insert(ucenterMember);
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }

    @Override
    public UcenterMember getByOpenid(String openid) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        return member;
    }




}
